//
//  PPTools.m
//  PgyHelper
//
//  Created by 鹏鹏 on 2020/12/18.
//  Copyright © 2020 CleverPeng. All rights reserved.
//

#import "PPTools.h"
#import "AmIBeingDebugged.h"

@implementation PPTools

+ (void)jumpToDownloadPage:(NSString *)urlString buildPassword:(NSString *)buildPassword {
    NSString *message = [NSString stringWithFormat:@"即将打开地址: %@, 密码: %@", urlString, buildPassword];
    if (buildPassword.length > 0) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = buildPassword;
        message = [NSString stringWithFormat:@"即将打开地址: %@, 密码: %@已复制到粘贴板", urlString, buildPassword];
    } else {
        message = [NSString stringWithFormat:@"即将打开地址: %@", urlString];
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [UIApplication.sharedApplication openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [UIApplication.sharedApplication.windows.firstObject.rootViewController presentViewController:alert animated:YES completion:nil];
}

+ (void)jumpToInstallDirectly:(NSString *)buildKey buildPassword:(NSString *)buildPassword {

    BOOL isMac = YES;

    /// 是否是调试模式(模拟器调试/mac调试, 都返回yes)
    BOOL isDebugged = AmIBeingDebugged();
    NSString *message_ = @"该设备不支持直接安装app";

    void(^alertCanontInstall)(NSString *message) = ^(NSString *message){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]];
        [UIApplication.sharedApplication.windows.firstObject.rootViewController presentViewController:alert animated:YES completion:nil];
    };

    if (isDebugged) {
        message_ = @"调试模式下不支持直接安装app";
        alertCanontInstall(message_);
        return;
    }
    /// 利用这种方式判断是不是mac端, 若一定时间之后, 该值未变化, 表示当前是mac系统
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (isMac) {
            alertCanontInstall(message_);
        }
    });

#if TARGET_OS_MACCATALYST
    // 如果是mac端  // #if !TARGET_OS_MACCATALYST // 如果不是mac端
    return;
#endif
    // 没有return说明不是mac端
    isMac = NO;
    NSString *installUrl = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://www.pgyer.com/app/plist/%@", buildKey];
    NSString *message = [NSString stringWithFormat:@"即将访问%@以安装应用, 是否继续?", installUrl];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"安装应用" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [UIApplication.sharedApplication openURL:[NSURL URLWithString:installUrl] options:@{} completionHandler:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil]];
    [UIApplication.sharedApplication.windows.firstObject.rootViewController presentViewController:alert animated:YES completion:nil];

}

+ (NSString *)fileSizeFormat:(long long)value {

    if (value < 0) {
        return @"--";
    }

    NSString *sizeString = @"";

    NSArray *formatArray = @[@"B", @"KB", @"MB", @"GB", @"TB", @"PB"];
    NSInteger count = formatArray.count;

    NSInteger index = 0;
    double size = value;
    while (index < count && size >= 1024) {
        size = size / 1024;
        index ++;
    }

    // 输出当前 格式
    if (index == 0) {
        sizeString = [NSString stringWithFormat:@"%d%@", (int)size, formatArray[index]];
    } else {
        sizeString = [NSString stringWithFormat:@"%@%@", [self removeFloatAllZeroByString:[NSString stringWithFormat:@"%.2f", size]], formatArray[index]];
    }

    return sizeString;
}

+ (NSString*)removeFloatAllZeroByString:(NSString *)testNumber {
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

@end
